package com.cxm.helper.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxm.helper.mapper.ArticleMapper;
import com.cxm.helper.pojo.Category;
import com.cxm.helper.pojo.User;
import com.cxm.helper.pojo.dto.CategoryListDTO;
import com.cxm.helper.pojo.dto.CategorySaveDTO;
import com.cxm.helper.pojo.vo.ArticleNumGroupVO;
import com.cxm.helper.pojo.vo.CategoryVO;
import com.cxm.helper.service.ArticleService;
import com.cxm.helper.service.CategoryService;
import com.cxm.helper.mapper.CategoryMapper;
import com.cxm.helper.service.UserService;
import com.cxm.helper.util.BeanUtil;
import com.cxm.helper.util.response.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author chenximeng
* @description 针对表【category】的数据库操作Service实现
* @createDate 2022-11-23 14:07:17
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserService userService;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageResponse listCategory(CategoryListDTO categoryListDTO) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(categoryListDTO.getKeyword())) {
            wrapper.like("name", categoryListDTO.getKeyword());
        }
        Page<Category> page = new Page<>(-1, -1);
        if (categoryListDTO.getCurrent() != null) {
             page = new Page<>(categoryListDTO.getCurrent(), categoryListDTO.getSize());
        }
        IPage<Category>  result = categoryMapper.selectPage(page, wrapper);

        List<Category> categories = result.getRecords();
        if (CollUtil.isEmpty(categories)){
            return new PageResponse<CategoryVO>(result.getCurrent(), result.getTotal(), result.getSize(), CollUtil.newArrayList());
        }
        List<CategoryVO> list = BeanUtil.copyList(categories, CategoryVO.class);
        List<Long> categoryIds = list.stream().map(CategoryVO::getId).collect(Collectors.toList());
        List<ArticleNumGroupVO> numsList = articleMapper.listArticleNumByCategoryIds(categoryIds);
        List<Long> userIds = list.stream().map(CategoryVO::getCreateUser).distinct().collect(Collectors.toList());
        Map<Long, String> userMap = userService.listByIds(userIds).stream().collect(Collectors.toMap(User::getId, User::getNickname));
        Map<Long, Long> articleNumMap = numsList.stream().collect(Collectors.toMap(ArticleNumGroupVO::getCategoryId, ArticleNumGroupVO::getArticleNum));
        for (CategoryVO categoryVO : list) {
            categoryVO.setArticleNum(articleNumMap.get(categoryVO.getId()) == null ? 0 : articleNumMap.get(categoryVO.getId()));
            categoryVO.setCreateUserNickname(userMap.get(categoryVO.getCreateUser()));
        }
        list.sort(Comparator.comparing(CategoryVO::getArticleNum).reversed());
        return new PageResponse<>(page, list);
    }

    @Override
    public void saveOrUpdate(CategorySaveDTO categorySaveDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categorySaveDTO, category);
        if (categorySaveDTO.getId() == null) {
            category.setCreateUser(StpUtil.getLoginIdAsLong());
            save(category);
            return;
        }
        updateById(category);

    }
}




